package com.atguigu.srb.core.controller.api;


import com.atguigu.srb.base.result.R;
import com.atguigu.srb.base.utils.JwtUtils;
import com.atguigu.srb.core.pojo.entity.UserAccount;
import com.atguigu.srb.core.pojo.entity.UserInfo;
import com.atguigu.srb.core.pojo.entity.UserLoginRecord;
import com.atguigu.srb.core.pojo.vo.UserIndexVO;
import com.atguigu.srb.core.pojo.vo.UserInfoVO;
import com.atguigu.srb.core.pojo.vo.UserResponseVO;
import com.atguigu.srb.core.service.UserAccountService;
import com.atguigu.srb.core.service.UserInfoService;
import com.atguigu.srb.core.service.UserLoginRecordService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 用户基本信息 前端控制器
 * </p>
 *
 * @author Atguigu
 * @since 2023-01-04
 */
@RestController

@RequestMapping("/api/core/userInfo")
public class ApiUserInfoController {
    @Resource
    UserInfoService userInfoService;
    @Resource
    UserAccountService userAccountService;
    @Resource
    UserLoginRecordService userLoginRecordService;
    @ApiOperation("个人中心首页数据")
    @GetMapping("auth/userIndex")
    public R getUserIndexVO(@RequestHeader(value = "token" , required = false) String token){
        Long userId = JwtUtils.getUserId(token);

        UserInfo userInfo = userInfoService.getById(userId);
        UserAccount userAccount = userAccountService.getOne(Wrappers.lambdaQuery(UserAccount.class)
                .eq(UserAccount::getUserId, userId));
        UserLoginRecord userLoginRecord = userLoginRecordService.getOne(Wrappers.lambdaQuery(UserLoginRecord.class)
                .eq(UserLoginRecord::getUserId, userId)
                .select(UserLoginRecord::getCreateTime)
                .orderByDesc(UserLoginRecord::getCreateTime)
                .last("limit 0 , 1"));


        UserIndexVO userIndexVO = new UserIndexVO();
        userIndexVO.setUserType(userInfo.getUserType());
        userIndexVO.setName(userInfo.getName());
        userIndexVO.setBindStatus(userInfo.getBindStatus());
        userIndexVO.setHeadImg(userInfo.getHeadImg());
        userIndexVO.setAmount(userAccount.getAmount());
        userIndexVO.setFreezeAmount(userAccount.getFreezeAmount());
        userIndexVO.setLastLoginTime(userLoginRecord.getCreateTime());
        return R.ok().data("item" , userIndexVO);
    }

    @ApiOperation("手机号重复注册验证")
    @GetMapping("checkMobile/{mobile}")
    public R checkMobile(@PathVariable("mobile") String mobile){
        int count = userInfoService.count(Wrappers.lambdaQuery(UserInfo.class)
                .eq(UserInfo::getMobile, mobile));
        return R.ok().data("flag" , count==0);//true表示可用
    }

    @ApiOperation("token验证")
    @GetMapping("auth/checkToken")
    public R checkToken(@RequestHeader(value = "token",
            required = false)String token){//以后前端提交token通过请求头中的token来提交
        //验证token
        boolean b = JwtUtils.checkToken(token);
        return R.ok().data("flag",b);//true表示token有效
    }
    /*
        1、接收前端登录用户的数据
        2、根据账号密码+用户类型查询数据库用户信息
        3、查询成功 登录成功
        4、将用户信息封装为jwt token+页面需要回显数据的响应

        自定义响应类：
            String token; // jwt格式
            String nickName;
            Integer userType;
            String headImg;
     */
    @ApiOperation("用户认证")
    @PostMapping("login")
    public R login(@RequestBody UserInfoVO userInfoVO, HttpServletRequest request){
        UserResponseVO userResponseVo = userInfoService.login(userInfoVO,request);
        return R.ok().data("userInfo",userResponseVo);
    }


    /*
        如果java代码中需要接受响应数据，没有现有的bean可以封装数据
        1、可以基于现有的bean扩展
        2、可以自定义新的bean封装数据
     */
    @ApiOperation("注册")
    @PostMapping("register")
    public R register(@RequestBody UserInfoVO userInfo){
        //处理注册的业务
        userInfoService.register(userInfo);
        return R.ok();
    }

}

